package zdh.avatar.base;

import java.io.File;
import java.io.IOException;

import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.SourceDataLine;
import javax.sound.sampled.UnsupportedAudioFileException;


public class Audio {
    
    private File audioFile;
    private AudioFormat format;
    private AudioInputStream audioStream;
    private DataLine.Info info;
    private SourceDataLine line;
    
    public Audio(String filePath){
        audioFile = new File(filePath);
        line = null;
        initAudio();
    }
    
    private void initAudio(){
        try {
            audioStream = AudioSystem.getAudioInputStream(audioFile);
            format = audioStream.getFormat();
            info = new DataLine.Info(SourceDataLine.class, format);
            
            if(AudioSystem.isLineSupported(info)){
                line = (SourceDataLine) AudioSystem.getLine(info);
                line.open(format);
                line.start();
            }
            
        } catch (UnsupportedAudioFileException | IOException | LineUnavailableException ex) {
        }
    }
    
}
